package com.hzit.cart.controller;

import com.hzit.cart.domain.ItemDTO;
import com.hzit.cart.service.CartService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping
public class CartController {
	@Autowired
	private CartService cartService;
	/**
	 * 1. 展示购物车信息
	 * @return
	 */
	@GetMapping({"/","/cart.html"})
	public String index(Model model){
		Map<String, List<ItemDTO>> itemDTOs = cartService.findCartByLogin();
		model.addAttribute("cartList",itemDTOs);
		return "cart";
	}


	/**
	 * 2. 添加购物车
	 */
	@GetMapping("/addCart/{id}/{num}")
	@ResponseBody
	public R<ItemDTO> addCart(@PathVariable("id") Long id, @PathVariable("num") Integer num){
		// 2.1 添加商品到购物车中
		ItemDTO dto = cartService.addCart(id, num);
		return R.ok(dto);
	}
}
